﻿ /*  作者：       tianzh
 *  创建时间：   2012/8/5 19:02:52
 *
 */

using System;
using System.Web.Mvc;
using TZHSWEET.Common;

namespace Command
{
    /// <summary>
    ///  
    /// </summary>
     [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
    public class LogExceptionFilterAttribute : ActionFilterAttribute
    {
        public string Message { get; set; }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {  
            if (filterContext.Exception != null)
            {
               
                    filterContext.Controller.ViewData["ErrorMessage"] = String.Format(@"<b>异常消息: {0}</br><b>触发Action:  </p>{1}</br><b>异常类型:  </b>{2}",
                        filterContext.Exception.GetBaseException().Message,
                        filterContext.ActionDescriptor.ActionName,
                        filterContext.Exception.GetBaseException().GetType().ToString());
                    LogHelper.WriteLog("系统错误:" + filterContext.Controller.ViewData["ErrorMessage"].ToString());
                    filterContext.Result = new ViewResult()
                    {
                        ViewName = "Error",
                        ViewData = filterContext.Controller.ViewData,
                    };

               
                filterContext.ExceptionHandled = true;
            }
        }

        //public override void OnActionExecuting(ActionExecutingContext filterContext)
        //{ //在Action执行前执行
        //    filterContext.HttpContext.Response.Write(@"<br />Before Action execute" + "\t " + Message);
        //    base.OnActionExecuting(filterContext);
        //}

        //public override void OnResultExecuted(ResultExecutedContext filterContext)
        //{ //在Result执行之后 
        //    filterContext.HttpContext.Response.Write(@"<br />After ViewResult execute" + "\t " + Message);
        //    base.OnResultExecuted(filterContext);
        //}

        //public override void OnResultExecuting(ResultExecutingContext filterContext)
        //{ //在Result执行之前
        //    filterContext.HttpContext.Response.Write(@"<br />Before ViewResult execute" + "\t " + Message);
        //    base.OnResultExecuting(filterContext);
        //}
    }
}
